<?php 
class Core_Validate_IDCard extends Zend_Validate_Abstract
{

	protected $_options;
	protected $_card;
	
	public function __construct($options)
	{
		if ($options instanceof Zend_Config)
		{
			$this->_options = $options->toArray();
		}
		else
		{
			$this->_options = $options;
		}
		
		if (isset($this->_options['card']))
		{
			$this->setCard($this->_options['card']);
		}
	}
	
	
	protected function setCard($value)
	{
		$this->_card = $value;
	}
	
	public function isValid($value)
	{
		if ($this->checksfznum($value) == true)
		{
			return true;
		}
		return false;
	}
	
/**
 * 检查身份证号码
 * 参数：15位或18位身份证号码，可选参数 $len 可以15位号码。
 * 返回值：失败返回FALSE，成功返回一个18位的身份证号
 */
private function checksfznum ($sfznum,$len='both') {
 if(strlen($sfznum)==15&&$len=='both'){    //当$len不等于'both'时，15位号码无效
  $truenum=substr($sfznum,0,6).'19'.substr($sfznum,6);             //为返回18位号码作准备。
  $preg="/^[\d]{8}((0[1-9])|(1[0-2]))((0[1-9])|([12][\d])|(3[01]))[\d]{3}$/";
 }elseif(strlen($sfznum)==18){
  $truenum=substr($sfznum,0,17);
  $preg="/^[\d]{6}((19[\d]{2})|(200[0-8]))((0[1-9])|(1[0-2]))((0[1-9])|([12][\d])|(3[01]))[\d]{3}[0-9xX]$/";
 }else{
  return false;
 }
 if(!preg_match($preg,$sfznum))return false;   //完成正则表达式检测

 /*----------- 以下计算第18位验证码-------------*/
 $nsum=      substr($truenum, 0,1)*7;
 $nsum=$nsum+substr($truenum, 1,1)*9;
 $nsum=$nsum+substr($truenum, 2,1)*10;
 $nsum=$nsum+substr($truenum, 3,1)*5;
 $nsum=$nsum+substr($truenum, 4,1)*8;
 $nsum=$nsum+substr($truenum, 5,1)*4;
 $nsum=$nsum+substr($truenum, 6,1)*2;
 $nsum=$nsum+substr($truenum, 7,1)*1;
 $nsum=$nsum+substr($truenum, 8,1)*6;
 $nsum=$nsum+substr($truenum, 9,1)*3;
 $nsum=$nsum+substr($truenum,10,1)*7;
 $nsum=$nsum+substr($truenum,11,1)*9;
 $nsum=$nsum+substr($truenum,12,1)*10;
 $nsum=$nsum+substr($truenum,13,1)*5;
 $nsum=$nsum+substr($truenum,14,1)*8;
 $nsum=$nsum+substr($truenum,15,1)*4;
 $nsum=$nsum+substr($truenum,16,1)*2;
 $yzm=12-$nsum%11;
 if($yzm==10)$yzm='x';
 elseif($yzm==12)$yzm='1';
 elseif($yzm==11)$yzm='0';
 /*----------18位验证码计算完成-------------*/
 if(strlen($sfznum)==18){
  if(substr($sfznum,17,1)!=$yzm)return false;
 }
 return $truenum.$yzm;
}
	
	
}